package arithmetic

import (
	"gitee.com/huanminabc/go-StrMathCalculate/register"
	"gitee.com/huanminabc/go-StrMathCalculate/structure"
	"gitee.com/huanminabc/go-utils/common"
	"strconv"
)

type RemainderArithmeticCalculate struct {
	name          string                  //计算器名称
	priority      int                     //优先级
	calculateData structure.CalculateData //计算器数据
	result        string                  //计算结果

}

func init() {
	//注册计算器
	register.RegisterCalculate(&RemainderArithmeticCalculate{
		name:     "%",
		priority: 1,
	})
}

func (a *RemainderArithmeticCalculate) GetName() string {
	return a.name
}

func (a *RemainderArithmeticCalculate) IsBool(symbol string) bool {
	if symbol == a.name {
		// 判断是否是整数
		b1 := common.IsInt(a.calculateData.A)
		b2 := common.IsInt(a.calculateData.B)
		if b1 && b2 {
			//判断模是否为0
			if a.calculateData.B == "0" {
				panic("模不能为0: " + a.calculateData.A + "%" + a.calculateData.B)
			}
			return true
		}
		panic("模运算必须是整数: " + a.calculateData.A + "%" + a.calculateData.B)
	}
	return false
}

func (a *RemainderArithmeticCalculate) AddExpression(a1, symbol, b1 string) {
	a.calculateData = structure.CalculateData{
		A:      a1,
		Symbol: symbol,
		B:      b1,
	}
}

func (a *RemainderArithmeticCalculate) Calculate() {
	A1, _ := strconv.Atoi(a.calculateData.A)
	B1, _ := strconv.Atoi(a.calculateData.B)
	//取模A1%B1 本质上是A1-A1/B1*B1
	a.result = strconv.Itoa(A1 - A1/B1*B1)
}

func (a *RemainderArithmeticCalculate) GetResult() string {
	return a.result
}

func (a *RemainderArithmeticCalculate) GetPriority() int {
	return a.priority
}
